package com.fmatos.amazingwallpapers;
 
import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.joda.time.LocalTime;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.fmatos.amazingwallpapers.downloader.BackgroundManagerForAndroid;
import com.fmatos.amazingwallpapers.downloader.WallpaperCache;
import com.fmatosqg.gatherimages.backgroundmanager.BackgroundManagerFactory;
import com.fmatosqg.gatherimages.backgroundmanager.OSValidator;

public class AmazingWallpapers extends Activity {



    private WallpaperCache wallpaperCache;
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	
    	// Background manager provides ways to get the default app folder, as well as 
    	// set the background
    	BackgroundManagerFactory.setAndroidManager(new BackgroundManagerForAndroid(getApplicationContext()));
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hello_layout);
        
        
    }

    @Override
    public void onStart() {
        super.onStart();
        LocalTime currentTime = new LocalTime();
        EditText edit = (EditText) findViewById(R.id.editText1);
    	edit.setText("Start");
        edit.setText("The current local time is: " + currentTime + "  / os is " + OSValidator.getOSName() + OSValidator.isAndroid() );
        
    }

	public void runme(View view) {
		
		Toast.makeText(this, "Downloading some images",Toast.LENGTH_LONG).show();
		
		ImageView imgView = (ImageView) findViewById(R.id.imageView1);
		imgView.setImageResource(R.drawable.ic_launcher);
		
		
		EditText edit = (EditText) findViewById(R.id.editText1);
		ImageView image = (ImageView) findViewById(R.id.imageView1);
		
		wallpaperCache = new WallpaperCache(edit, image, this);
		wallpaperCache.execute((String)null);
		
		Toast.makeText(this, "Downloading is done",Toast.LENGTH_LONG).show();
			
	}	
	
	public void setWallpaper(View view) {
		 	
		if ( wallpaperCache == null ) {
			Toast.makeText(this, "Wallpaper is not prepared",Toast.LENGTH_SHORT).show();
			return;
		}
		
		List<String> list = wallpaperCache.getCachedImages();
		
		if ( list != null && list.size() > 0) {
			String file = list.get(0);
			
			Toast.makeText(this, "Setting wallpaper",Toast.LENGTH_SHORT).show();
			BackgroundManagerFactory.getInstance().setBackgroundImage(file);
		} else {
			Toast.makeText(this, "No image available",Toast.LENGTH_SHORT).show();
		}

	}
	

	
	public void runProgressBar(View view) {
		new SimpleProgressBar().execute(null,null,null);
	}
	
	private ProgressDialog m_dialog;
	public void testProgressDialog(View view) {
		 
		m_dialog = new ProgressDialog(this);
		new SimpleProgressDialog().execute();
	}
	

	
	/**
	 * Displays simple animation in progress bar 
	 * @author fabio.dematos
	 *
	 */
	private class SimpleProgressBar extends AsyncTask<URL, Integer, Long> {
		
		private final static int max = 100;
		private final static int millis = 20;
		
		
		protected int getMax() {
			return max;
		}
		
	    protected Long doInBackground(URL ... urls) {
	    	
	    	int i =0;
	    	
	    	for ( i = 0 ; i < max ; i++) {
	    		try {
					Thread.sleep(millis);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	    		publishProgress(i);
	    		if (isCancelled()) break;
	    	}
	    	
	    	return Long.valueOf(i);
	    }

	    protected void onPreExecute() {
	    	EditText edit = (EditText) findViewById(R.id.editText1);
	    	edit.setText("Start");
	    	ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
	    	bar.setMax(max);
	    	bar.setProgress(0);
	    	
	    	ProgressBar bar2 = (ProgressBar) findViewById(R.id.progressBar2);
	    	bar2.setVisibility(ProgressBar.VISIBLE);
	    	
	    }
	    
	    protected void onProgressUpdate(Integer ... progress) {
	    	EditText edit = (EditText) findViewById(R.id.editText1);
	    	edit.setText("count = "  + progress[0].intValue() );
	    	
	    	ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
	    	bar.setProgress(progress[0]);
	    }

	    protected void onPostExecute(Long result) {
	    	EditText edit = (EditText) findViewById(R.id.editText1);
	    	edit.setText("Done");
	    	
	    	ProgressBar bar = (ProgressBar) findViewById(R.id.progressBar1);
	    	bar.setVisibility(ProgressBar.INVISIBLE);
	    	
	    	ProgressBar bar2 = (ProgressBar) findViewById(R.id.progressBar2);
	    	bar2.setVisibility(ProgressBar.INVISIBLE);
	    	bar2.setVisibility(ProgressBar.GONE);
	    }
	}


	public class SimpleProgressDialog extends SimpleProgressBar {



		protected void onPreExecute() {
			m_dialog.setTitle("Searching...");
			m_dialog.setMessage("Please wait while searching...");
			m_dialog.setIndeterminate(true);
			m_dialog.setCancelable(true);
			m_dialog.show();
			m_dialog.setMax(100);
		}

		protected void onPostExecute(Long result) {
			EditText edit = (EditText) findViewById(R.id.editText1);
			edit.setText("ProgressDialog Done");

			m_dialog.setMax(getMax());
			m_dialog.dismiss();

		}

		protected void onProgressUpdate(Integer ... progress) {

			m_dialog.setProgress(progress[0]);
			super.onProgressUpdate(progress);
		}

	}
}